package net.kts.transrate;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import net.kts.setting.language.LanguageUtil;
import android.util.Log;

import com.memetix.mst.MicrosoftTranslatorAPI;
import com.memetix.mst.language.Language;
import com.memetix.mst.translate.Translate;

public class TranslateService {
	private static final String apiKey = "E2E42A43C29EFED999BC41591287325177EC1566";
	private static HashMap<String, String> textMap;
	static{
		MicrosoftTranslatorAPI.setKey(apiKey);
	}
	public static void setTranslateTextMap(Map<String,String> wordMap){
//		Collection<String> values = wordMap.values();
//		Iterator<String> iterator = values.iterator();
//		while(iterator.hasNext()){
//			Log.d("kts", iterator.next());
//		}
		textMap = (HashMap<String, String>) wordMap;
	}
	public static String translate(String fromText){
		try {
			if(LanguageUtil.mustBeTranslations()){
				String result = Translate.execute(fromText, 
							    				   Language.fromString(Language.AUTO_DETECT.toString())
							    				 , Language.fromString(LanguageUtil.getCurrentLanguage()));
				return result;
			}
		} catch (Exception e) {
			Log.d("kts", e.getMessage());
		}
		return fromText;
	}
	public static String translateRepository(String fromText){
		if(textMap.containsKey(fromText)){
			return textMap.get(fromText);
		}
		String result = translate(fromText);
		textMap.put(fromText, result);
		
	    LanguageUtil.storeTranslateWords(textMap);
		return result;
	}
	
	public static String translateToKorean(String fromText) {
		try {
			if(LanguageUtil.mustBeTranslations()){
				return Translate.execute(fromText, 
						Language.fromString(Language.AUTO_DETECT.toString())
						, Language.KOREAN);
			}
		} catch (Exception e) {
			Log.d("kts", e.getMessage());
		}
		return fromText;
	}
}
